package cn.itsource.pethome.user.service.impl;

import cn.itsource.pethome.basic.constant.PetHomeConstant;
import cn.itsource.pethome.basic.service.impl.BaseServiceImpl;
import cn.itsource.pethome.basic.util.MD5Utils;
import cn.itsource.pethome.basic.util.StrUtils;
import cn.itsource.pethome.user.domain.LoginInfo;
import cn.itsource.pethome.user.domain.User;
import cn.itsource.pethome.user.domain.dto.UserDto;
import cn.itsource.pethome.user.mapper.LoginInfoMapper;
import cn.itsource.pethome.user.mapper.UserMapper;
import cn.itsource.pethome.user.service.IUserservice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@Service
public class UserserviceImpl extends BaseServiceImpl<User> implements IUserservice {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Override
    public void register(UserDto userDto) {
        //校验前端传递的数据是否为空
        //校验手机号是否已被注册
        //校验2次密码是否一致
        //校验判断验证码是否过期    78De:3424234242
        //校验前端传递的验证码是否和redis中存储的验证码一致
        checkUserDto(userDto);

        //将UserDto转为logininfo对象
        LoginInfo loginInfo = userDto2loginInfo(userDto);
        //保存loginInfo
        loginInfoMapper.save(loginInfo);
        //将loginInfo对象转为User对象
        User user = loginInfo2User(loginInfo);
        //保存user
        userMapper.save(user);


    }

    /**
     * 将登录信息对象转为用户对象
     * @param loginInfo  登录信息对象
     * @return 用户信息
     */
    private User loginInfo2User(LoginInfo loginInfo) {
        User user = new User();
        //只要属性相同就能拷贝
        BeanUtils.copyProperties(loginInfo, user);
        //设置用户状态0: 待激活    1:正常
        user.setState(PetHomeConstant.OK);
        //设置用户登录信息
        user.setLoginInfo(loginInfo);

        return user;

    }

    /**
     * 将UserDto临时对象转为登录信息对象
     * @param userDto  临时对象
     * @return
     */
    private LoginInfo userDto2loginInfo(UserDto userDto) {
        LoginInfo loginInfo = new LoginInfo();
        //获取注册电话
        loginInfo.setPhone(userDto.getPhone());
        //获取用户名
        loginInfo.setUsername(userDto.getPhone());
        //获取盐值
        loginInfo.setSalt(StrUtils.getComplexRandomString(10));
        //获取密码
        loginInfo.setPassword(MD5Utils.encrypByMd5(userDto.getPassword()+loginInfo.getSalt()));
        //标记为前端用户
        loginInfo.setType(true);
        return loginInfo;
    }

    private void checkUserDto(UserDto userDto) {
        //校验前端传递的数据是否为空
        if (StringUtils.isEmpty(userDto.getPhone())||StringUtils.isEmpty(userDto.getCode())||
            StringUtils.isEmpty(userDto.getPassword())||StringUtils.isEmpty(userDto.getConfigPassword()))
        {
            throw new RuntimeException("请填写完整内容");
        }
        //校验手机号是否已被注册
        User user = userMapper.findByphone(userDto.getPhone());
        if (user!=null){
            throw new RuntimeException("该手机号码已被注册");
        }
        //校验2次密码是否一致
        if (!userDto.getPassword().equals(userDto.getConfigPassword())){

            throw new RuntimeException("两次输入密码不一致");
        }
        //校验判断验证码是否过期    78De:3424234242
        String codetime = (String)redisTemplate.opsForValue().get(PetHomeConstant.REGISTER_VERIFICATION_CODE + ":" + userDto.getPhone());
        if (StringUtils.isEmpty(codetime)){
            throw new RuntimeException("验证码已过期");
        }
        //校验前端传递的验证码是否和redis中存储的验证码一致
        String code = codetime.split(":")[0];
        if (!code.toLowerCase().equals(userDto.getCode().toLowerCase())){
            throw new RuntimeException("验证码有误");
        }
    }
}
